import functools
class Solution(object):
    def maxUncrossedLines(self, nums1, nums2):
        n1 = len(nums1) - 1
        n2 = len(nums2) - 1
        @functools.cache
        def search(i, j):
            if i == n1:
                if j == n2:
                    if nums1[i] == nums2[j]:
                        return 1
                    else:
                        return 0
                else:
                    if nums1[i] == nums2[j]:
                        return 1
                    else:
                        return search(i, j + 1)
            else:
                if j == n2:
                    if nums1[i] == nums2[j]:
                        return 1
                    else:
                        return search(i + 1, j)
                else:
                    if nums1[i] == nums2[j]:
                        return 1 + search(i + 1, j + 1)
                    else:
                        return max(search(i, j + 1), search(i + 1, j))

        return search(0, 0)
